package viewPackage.menus;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseEvent;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import mainPackage.GFrame;
import mainPackage.Resource;

@SuppressWarnings("serial")
public class VideoMenu extends JPanel {

	private MenuLabel resolutionValue;
	private int screenHeight;
	private int screenWidth;
	private boolean isFullScreen;
	private Resource resource;
	private JRadioButton windowed;
	private JRadioButton fullscreen;
	
	private GFrame gameFrame;
	
	public VideoMenu(GFrame mainFrame) {
		super();
		gameFrame = mainFrame;
		this.setBackground(Color.BLACK);
		
		MenuLabel title = new MenuLabel("Video Configuration", 48);
		
		MenuLabel windowedLabel = new MenuLabel("Windowed", 18);
		MenuLabel fullscreenLabel = new MenuLabel("Full Screen", 18);
		
		windowed = new JRadioButton();
		fullscreen = new JRadioButton();
		windowed.setBackground(Color.BLACK);
		fullscreen.setBackground(Color.BLACK);
		ButtonGroup group = new ButtonGroup();
		group.add(windowed);
		group.add(fullscreen);
		
		MenuLabel resolutionLabel = new MenuLabel("Resolution: ", 20);
		resolutionValue = new MenuLabel(null, 20);
		
		int sliderValue = 0;
		
		resource = new Resource();
		screenWidth = resource.screenWidth;
		screenHeight = resource.screenHeight;
		isFullScreen = resource.isFullScreen;
		
		if(isFullScreen){
			fullscreen.setSelected(true);
			windowed.setSelected(false);
		}else if(!isFullScreen){
			fullscreen.setSelected(false);
			windowed.setSelected(true);
		}
		
		if(screenWidth == 800 && screenHeight == 600) {
			sliderValue = 0;
			resolutionValue.setText("800 x 600");
		}
		else if(screenWidth == 1024 && screenHeight == 768) {
			sliderValue = 1;
			resolutionValue.setText("1024 x 768");
		}
		else if(screenWidth == 1280 && screenHeight == 720) {
			sliderValue = 2;
			resolutionValue.setText("1280 x 720");
		}
		else if(screenWidth == 1280 && screenHeight == 768) {
			sliderValue = 3;
			resolutionValue.setText("1280 x 768");
		}
		else if(screenWidth == 1280 && screenHeight == 800) {
			sliderValue = 4;
			resolutionValue.setText("1280 x 800");
		}
		else if(screenWidth == 1280 && screenHeight == 1024) {
			sliderValue = 5;
			resolutionValue.setText("1280 x 1024");
		}
		else if(screenWidth == 1440 && screenHeight == 900) {
			sliderValue = 6;
			resolutionValue.setText("1440 x 900");
		}
		else if(screenWidth == 1600 && screenHeight == 900) {
			sliderValue = 7;
			resolutionValue.setText("1600 x 900");
		}
		
		ResolutionSlider resolutionSlider = new ResolutionSlider(sliderValue);
		
		resolutionSlider.addChangeListener(new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent event) {
				JSlider slider = (JSlider) event.getSource();
				updateResolution(slider.getValue());				
			}});
		
	
		MenuLabel okLabel = new MenuLabel("OK", 30);
		MenuLabel cancelLabel = new MenuLabel("Cancel", 30);
		
		okLabel.addMouseListener(new JLabelMouse(okLabel) {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				updateFullScreenValue();
				updateResolutionValue();
				gameFrame.getContentPane().removeAll();
				gameFrame.getContentPane().add(new OptionsPanel(gameFrame), BorderLayout.CENTER);
				gameFrame.validate();
				gameFrame.repaint();				
				
			}});
		
		cancelLabel.addMouseListener(new JLabelMouse(cancelLabel) {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				gameFrame.getContentPane().removeAll();
				gameFrame.getContentPane().add(new OptionsPanel(gameFrame), BorderLayout.CENTER);
				gameFrame.validate();
				gameFrame.repaint();	
				
			}});
		
				
		this.setLayout(new GridBagLayout());
		
		Constraints Ctitle = new Constraints(0,0,2,1,true);
		Constraints CwinLab = new Constraints(0,1,1,1,false);
		Constraints CwinRB = new Constraints(0,2,1,1,false);
		Constraints CfsLab = new Constraints(1,1,1,1,false);
		Constraints CfsRB = new Constraints(1,2,1,1,false);
		Constraints CresLab = new Constraints(0,3,1,1,false);
		Constraints CresSize = new Constraints(1,3,1,1,false);
		Constraints Cslider = new Constraints(0,4,2,1,false);
		Cslider.fill = GridBagConstraints.HORIZONTAL;
		Constraints Cok = new Constraints(0,5,1,1,false);
		Constraints Ccancel = new Constraints(1,5,1,1,false);
		
		this.add(title, Ctitle);
		this.add(windowedLabel, CwinLab);
		this.add(fullscreenLabel, CfsLab);
		this.add(windowed, CwinRB);
		this.add(fullscreen, CfsRB);
		this.add(resolutionLabel, CresLab);
		this.add(resolutionValue, CresSize);
		this.add(resolutionSlider, Cslider);
		this.add(okLabel, Cok);
		this.add(cancelLabel, Ccancel);
				
	}
	
	private void updateFullScreenValue() {
		if(fullscreen.isSelected()){
			isFullScreen = true;
		}
		
		if(windowed.isSelected()){
			isFullScreen = false;
		}
		
	}

	private void updateResolutionValue() {
		
		resource.updateFile(screenWidth, screenHeight, isFullScreen);
		if(isFullScreen){
			DisplayMode dm = new DisplayMode (screenWidth,screenHeight,32,60);
			GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
			GraphicsDevice gd = ge.getDefaultScreenDevice();
			gameFrame.dispose();
			gameFrame.setUndecorated(true);
			gd.setFullScreenWindow(gameFrame);
			gd.setDisplayMode(dm);
		}
		else {
			gameFrame.dispose();
			gameFrame.setBounds(gameFrame.getBounds().x, gameFrame.getBounds().y, screenWidth, screenHeight);
			gameFrame.setUndecorated(false);
			gameFrame.setVisible(true);
		}
		
	}

	private void updateResolution(int i) {
		if (i == 0) {
			resolutionValue.setText("800 x 600");
			resolutionValue.repaint();
			screenWidth = 800;
			screenHeight = 600;
		}	
		else if (i == 1) {
			resolutionValue.setText("1024 x 768");
			resolutionValue.repaint();
			screenWidth = 1024;
			screenHeight = 768;
		}
		else if (i == 2) {
			resolutionValue.setText("1280 x 720");
			resolutionValue.repaint();
			screenWidth = 1280;
			screenHeight = 720;
		}
		else if (i == 3) {
			resolutionValue.setText("1280 x 768");
			resolutionValue.repaint();
			screenWidth = 1280;
			screenHeight = 768;
		}
		else if (i == 4) {
			resolutionValue.setText("1280 x 800");
			resolutionValue.repaint();
			screenWidth = 1280;
			screenHeight = 800;
		}
		else if (i == 5) {
			resolutionValue.setText("1280 x 1024");
			resolutionValue.repaint();
			screenWidth = 1280;
			screenHeight = 1024;
		}
		else if (i == 6) {
			resolutionValue.setText("1440 x 900");
			resolutionValue.repaint();
			screenWidth = 1440;
			screenHeight = 900;
		}
		else if (i == 7) {
			resolutionValue.setText("1600 x 900");
			resolutionValue.repaint();
			screenWidth = 1600;
			screenHeight = 900;
		}
	}
	
	
}
